package hr.bean;

import java.util.ArrayList;
import java.util.Date;

import hr.dao.DepartmentDAO;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import resources.bean.Login;
import resources.pojo.hr_department;
import resources.util.Tool;

@ManagedBean(name="department")
@SessionScoped
public class Department {
	private int currentPage;
	private hr_department department;
	private hr_department selectedDepartment;
	private String nameSearch;
	private ArrayList<hr_department> listDepartment;
	private Integer[] pages;
	private String last;
	
	public void onload() {
		if(!Tool.isPostBack()){
			init();
		}
	}
	
	public void initObject(){
		if(!Tool.isPostBack()){
			department = new hr_department();
		}
	}
	
	public void init(){
		currentPage = 1;
		nameSearch="";
		department = new hr_department();
		listDepartment = DepartmentDAO.getListDepartmentPaging("",currentPage,Tool.max_row);
		pages = Tool.getPages();
	}
	
	public void paging(){
		listDepartment = DepartmentDAO.getListDepartmentPaging("",currentPage,Tool.max_row);
		pages = Tool.getPages();
	}
	
	public void insert(){
		department.setId("DEP"+Tool.generateID());
		department.setStatus(1);
		department.setCreated_by(Login.user.getUsername());
		String result = DepartmentDAO.action("insert",department,0);
		if(result.equals("completed")){
		}else if(result.equals("duplicate")){
			Tool.message("Tên phòng ban không được trùng nhau");
		}
		init();
	}
	
	public void update(){
		selectedDepartment.setCreated_by(Login.user.getUsername());
		String result = DepartmentDAO.action("update",selectedDepartment,last.equals(selectedDepartment.getName())?1:0);
		if(result.equals("completed")){
		}else if(result.equals("duplicate")){
			Tool.message("Tên phòng ban không được trùng nhau");
		}
		init();
	}
	
	public void choose(ActionEvent event){
		setSelectedDepartment((hr_department) event.getComponent().getAttributes().get("object"));
		last = selectedDepartment.getName();
	}
	
	public void search(){
		listDepartment = DepartmentDAO.getListDepartmentPaging(nameSearch,1,Tool.max_row);
		setPages(Tool.getPages());
	}
	
	public void nextPage(){
		currentPage ++;
	}

	public void setDepartment(hr_department department) {
		this.department = department;
	}

	public hr_department getDepartment() {
		return department;
	}

	public ArrayList<hr_department> getListDepartment() {
		return listDepartment;
	}

	public void setListDepartment(ArrayList<hr_department> listDepartment) {
		this.listDepartment = listDepartment;
	}

	public String getNameSearch() {
		return nameSearch;
	}

	public void setNameSearch(String nameSearch) {
		this.nameSearch = nameSearch;
	}

	public void setSelectedDepartment(hr_department selectedDepartment) {
		this.selectedDepartment = selectedDepartment;
	}

	public hr_department getSelectedDepartment() {
		return selectedDepartment;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPages(Integer[] pages) {
		this.pages = pages;
	}

	public Integer[] getPages() {
		return pages;
	}

}
